//
//  QRCodeController.m
//  NPS
//
//  Created by jadd on 15/10/19.
//  Copyright © 2015年 jadd. All rights reserved.
//

#import "QRCodeController.h"
@interface QRCodeController ()
@property (nonatomic,strong) NSString *name;
@property (nonatomic,strong) NSURL *qrcodeLink;

@end

@implementation QRCodeController
@synthesize store;
@synthesize imageView;
@synthesize myTableView;


-(void)setUpView
{
    
    CGRect rect;
    rect = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    
    myTableView = [[UITableView alloc] initWithFrame:rect style:UITableViewStyleGrouped];
    //右边滚动条是否显示
    [myTableView setShowsVerticalScrollIndicator:NO];
    myTableView.separatorStyle = UITableViewCellSelectionStyleNone;

    myTableView.delaysContentTouches = NO;
    myTableView.delegate = self;
    myTableView.dataSource = self;
    //UITableView 去除空的Cell
    myTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    [self.view addSubview:myTableView];
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom] ;
    button.frame = CGRectMake(0, 0, K_Global_BackButton_Width, K_Global_BackButton_Height);
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [button setImage:[UIImage imageNamed:@"back_white_on"] forState:UIControlStateNormal];
    //[button setImage:[UIImage imageNamed:@"back_white_on"] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = leftButton;

                                   
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"预览问卷" style: UIBarButtonItemStylePlain target:self action:@selector(previewPressed:)];
    self.navigationItem.rightBarButtonItem = rightButton;
    
    
}

//保存相片到本地相册
- (void)saveAction:(id)sender {
    UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
}

//实现imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:方法
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSString *message = @"呵呵";
    if (!error) {
        message = @"成功保存到相册";
    }else
    {
        message = [error description];
    }
    [Config showMessage:message];

}



//预览问卷
-(IBAction)previewPressed:(id)sender
{
    PreviewWenjuanController *controller = [[PreviewWenjuanController alloc] init];
    controller.surveyLink = self.store.surveyLink;
    [self.navigationController pushViewController:controller animated:YES];
}

-(void)backButtonPressed{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    
    //self.edgesForExtendedLayout = UIRectEdgeNone;

    self.qrcodeLink = [NSURL URLWithString:self.store.qr_codeLink];
    self.name = self.store.name;
    self.title = @"问卷二维码";
    [self setUpView];
 
    //self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    //[self.tableView setShowsVerticalScrollIndicator:NO];
    [super viewDidLoad];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return K_ScreenHeight-40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *identifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.backgroundColor = BACK_VIEW_COLOR;
  
    //CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    CGRect rect = CGRectMake(0, 0, K_ScreenWidth, K_ScreenHeight);
    
    UIImageView *backImageView = [[UIImageView alloc] initWithFrame:rect];
    backImageView.userInteractionEnabled = YES;
    //backImageView.contentMode =  UIViewContentModeScaleAspectFit;
    [backImageView setImage:[UIImage imageNamed:@"QR-codes"]];
    [cell.contentView addSubview:backImageView];
    
    [backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        //make.height.equalTo(@150);
    //make.top.offset(5);
        
        make.top.mas_equalTo(cell.mas_top).with.offset(10);
        
        make.left.offset(15);
        make.right.offset(-15);
        
    }];

    UILabel *lable = [[UILabel alloc] init];
    lable.textAlignment = NSTextAlignmentCenter;
    lable.text = self.name;
    lable.textColor = WHITE;
    lable.adjustsFontSizeToFitWidth = YES;
    lable.font = [UIFont systemFontOfSize:22];
    [backImageView addSubview:lable];
    [lable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(backImageView.mas_top).with.offset(10);
        make.left.offset(30);
        make.right.offset(-30);
        
    }];
    
    imageView = [[UIImageView alloc] initWithFrame:rect];
    imageView.userInteractionEnabled = YES;
    imageView.contentMode =  UIViewContentModeScaleAspectFit;
    //[imageView setImageWithURL: self.qrcodeLink placeholderImage:[UIImage imageNamed:@"dianpu"]];
    [imageView setImageWithURL:self.qrcodeLink];
    [backImageView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.right.offset(-15);
        make.bottom.offset(-140);
        make.top.offset(60);
        
    }];

    
    UIButton* button = [UIButton buttonWithType:UIButtonTypeSystem];
    [backImageView addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.mas_equalTo(backImageView.mas_bottom).with.offset(-10);
        make.left.offset(20);
        make.right.offset(-20);
        
    }];

    
    button.backgroundColor = [UIColor clearColor];
    UIFont *font = [UIFont systemFontOfSize:18.0];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"保存到相册"];
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:K_Global_ColorHexString_Store] range:NSMakeRange(0, string.length)];
    [string addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, string.length)];

    [button setAttributedTitle:string forState:UIControlStateNormal];
    [button addTarget:self action:@selector(saveAction:) forControlEvents:UIControlEventTouchUpInside];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
  
    return cell;
    
}


#pragma mark - Table view delegate
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if(row == 0)
    {
        
    }
}


@end
